package cn.rokka.rbac.web.controller;


import cn.rokka.rbac.anno.RequiredPermission;
import cn.rokka.rbac.domain.Department;
import cn.rokka.rbac.qo.QueryObject;
import cn.rokka.rbac.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    @RequiredPermission(name = "部门查询",expression = "department:list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";
    }



    @RequestMapping("/delete")
    @RequiredPermission(name = "部门删除",expression = "department:delete")
    public String delete(Long id) {
        departmentService.delete(id);
        return "redirect:/department/list";
    }

    @RequestMapping("/input")
    @RequiredPermission(name = "部门编辑",expression = "department:input")
    public String input(Long id, Model model) {
        Department department = departmentService.getById(id);
        if (department != null) {
            model.addAttribute("department", department);
        }
        return "department/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name = "部门编辑",expression = "department:saveOrUpdate")
    public String saveOrUpdate(Department dept) {
        if (dept.getId() != null) {
            departmentService.update(dept);
        } else {
            departmentService.save(dept);
        }
        return "redirect:/department/list";
    }
}
